package com.devunion.bogdan.view.editors;

import com.devunion.bogdan.model.project.Annotation;
import com.devunion.bogdan.model.project.AnnotationType;
import com.devunion.bogdan.util.IconHelper;
import com.devunion.bogdan.util.ImageHelper;

import javax.swing.*;

import org.apache.commons.lang.StringUtils;

import java.awt.*;

/**
 * @author Viktar Liaskovich
 */
public class AnnotationView extends JLabel {
    public static final String EMPTY_TEXT = "(empty text)";
    private Annotation annotation;
    private boolean isInitialized = false;
    private Image myImage;
    private int realX;
    private int realY;
    private int realWidth;
    private int realHeight;
    private float ratio = 1.0f;

    public AnnotationView(Annotation annotation) {
        super((String) null);

        if (annotation == null) {
            throw new IllegalArgumentException("Annotation can't be null");
        }

        this.annotation = annotation;

        myImage = ImageHelper.getImage("AddImageAnnotation16.gif");

        if (annotation.getType() == AnnotationType.IMAGE) {
            setIcon(IconHelper.getIcon("AddImageAnnotation16.gif"));
        } else {
            setIcon(IconHelper.getIcon("AddTextAnnotation16.gif"));
        }

        isInitialized = true;
    }

    @Override
    public void resize(int width, int height) {
        realWidth = (int) (width / ratio);
        realHeight = (int) (height / ratio);
        super.resize(width, height);
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        realX = (int) (x / ratio);
        realY = (int) (y / ratio);
        realWidth = (int) (width / ratio);
        realHeight = (int) (height / ratio);

        super.setBounds(x, y, width, height);
    }

    public Annotation getAnnotation() {
        return annotation;
    }

    public void setRatio(float ratio) {
        this.ratio = ratio;

        super.setBounds(
                (int) (annotation.getX() * ratio),
                (int) (annotation.getY() * ratio),
                (int) (annotation.getWidth() * ratio),
                (int) (annotation.getHeight() * ratio)
        );
    }

    public int getRealX() {
        return realX;
    }

    public int getRealY() {
        return realY;
    }

    public int getRealWidth() {
        return realWidth;
    }

    public int getRealHeight() {
        return realHeight;
    }

    public void setText(String text) {
        if (isInitialized) {
            throw new UnsupportedOperationException("You can use getAnnotation().setText() to change annotation text only");
        }
    }

    protected void paintComponent(Graphics g) {
        Icon oldIcon = getIcon();

        if (oldIcon == null) {
            super.paintComponent(g);
            return;
        }

        setIcon(null);
        super.paintComponent(g);

        oldIcon.paintIcon(this, g, getWidth() - oldIcon.getIconWidth(), 0);
//        g.drawImage(myImage, getWidth() - oldIcon.getIconWidth(), 0, this);

        setIcon(oldIcon);
    }

    public String getText() {
        if (isInitialized) {
            return StringUtils.isBlank(annotation.getText()) ? EMPTY_TEXT : annotation.getText();
        }

        return super.getText();
    }
}
